from game_config import *
from game_menu import *
from game_server import *
from event_handler import *
import time
from player import *
from game_logic import *
import gooeypy as gui
from gooeypy.const import *
import pygame

class game:
    def __init__(self):
        self.next_status = ""
        self.game_status = "premenu"
        self.config = game_config()
        self.info_server_running = False
        self.game_logic = game_logic(self)
        self.event_handler = event_handler(self)
        self.players = {}
        self.local_player = None
        self.round = 1
        self.screen = None
        self.main_loop = True
        self.loop_function = self.premenu
        self.events = []
    
    def add_network_player(self, callsign, addr):
        self.players[len(self.players)] = player(callsign, "network", addr)
        
    def add_local_player(self):
        newPlayer = player(self.config.callsign, "local", ("127.0.0.1", 1976))
        self.players[len(self.players)] = newPlayer
        self.config.local_player = newPlayer
    
    def start_info_server(self):
        self.info_server_running = True
        self.info_server = game_info_server(self)
        self.info_server.start()
    
    def info_server_stopped(self):
        self.info_server_running = False
    
    def stop_info_server(self):
        self.info_server.stop()
        
    
    def run(self):
        while self.main_loop:
            self.events = pygame.event.get()
            self.event_handler.run()
            self.loop_function()
            self.events = []
        
        print "exitting"
    
    def premenu(self):
        print "Welcome to hell. It's 1976 and you're up against the Charlie."
        print "You got what it takes? Let's see what you got. First things first\n"
        self.next_status = "menu"
        self.config.load_cfg_file("data/tank.cfg")
        pygame.event.get()
        
        #set up screen
        self.screen = pygame.display.set_mode(self.config.options['resolution'])
        self.screen_rect = self.screen.get_rect()
        
        self.event_handler.connect(pygame.QUIT, self.stop_loop)
        self.event_handler.connect(pygame.USEREVENT, self.user_events)
        
        #set up gui
        gui.init(myscreen=self.screen)
        self.guiApp = gui.App(width=self.screen_rect.w, height=self.screen_rect.h)
        self.game_menu = game_menu(width=self.screen_rect.w, height=self.screen_rect.h)
        self.guiApp.add(self.game_menu)
        self.game_menu.setup(self)
        
        self.loop_function = self.menu
        return
    
    def menu(self):
        self.game_menu.run()
        self.guiApp.run(self.events)
        self.guiApp.draw()
        gui.update_display()
        return
    
    def start_game(self):
        print "\n\nSTART GAME"
        self.next_status = "post_game"
        if(self.config.host):
            self.game_logic.setup_host()
            self.game_logic.run_host()
        else:
            self.game_logic.setup_client()
            self.game_logic.run_client()
        self.round+= 1
        print "FINISHED RUNNING. GOING TO: %s" % self.next_status
    
    def post_game(self):
        print "Post Game"
        self.game_menu.run("post_game")
    
    def stop_loop(self, event=None):
        self.main_loop = False
        if self.config.host:
            self.info_server.stop()
    
    def user_events(self, event=None):
        details = event.details
        if details['type'] == 'game_launch':
            self.game_menu.active.active = False
            if self.config.host:
                self.game_logic.setup_host()
                self.loop_function = self.game_logic.run_host
            else:
                self.game_logic.setup_client()
                self.loop_function = self.game_logic.run_client
        
        elif details['type'] == "round_end":
            self.game_logic.stop()
            misc = {'type': 'round_lobby_start'}
            pygame.event.post(pygame.event.Event(pygame.USEREVENT, {'details':misc}))
        
        elif details['type'] == "round_lobby_start":
            self.game_menu.round_end()
            self.guiApp.dirty = True
            self.loop_function = self.menu
